package io.renren.modules.lanling.controller;

import java.util.*;

import io.renren.common.utils.Query;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.lanling.entity.SchoolEntity;
import io.renren.modules.lanling.entity.SchoolPicEntity;
import io.renren.modules.lanling.service.SchoolPicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.service.SchoolService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 学校
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-10-24 10:40:05
 */
@RestController
@RequestMapping("lanling/school")
public class ShoolController {
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private SchoolPicService schoolPicService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:school:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = schoolService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:school:info")
    public R info(@PathVariable("id") Integer id) {
        SchoolEntity shool = schoolService.queryObject(id);

        return R.ok().put("school", shool);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:school:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody SchoolEntity shool) {
        shool.setCreateDate(new Date());
        schoolService.insert(shool);

        Integer schoolId = shool.getId();
        List<SchoolPicEntity> schoolImgList = shool.getSchoolImgList();
        schoolPicService.saveSchoolImgList(schoolId, schoolImgList);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:school:update")
    @Transactional(rollbackFor = Exception.class)
    public R update(@RequestBody SchoolEntity shool) {
        ValidatorUtils.validateEntity(shool);

        Integer schoolId = shool.getId();

        //学校图册,修改时时全删全插
        schoolPicService.deleteBySchoolId(schoolId);
        List<SchoolPicEntity> schoolImgList = shool.getSchoolImgList();
        schoolPicService.saveSchoolImgList(schoolId, schoolImgList);

        shool.setLastModifyDate(new Date());
        schoolService.update(shool);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:school:delete")
    public R delete(@RequestBody Integer[] ids) {
        schoolService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll() {
        Map<String, Object> params = new HashMap<>();
        params.put("sidx", "school_name");
        params.put("order", "desc");
        Query query = new Query(params);
        List<SchoolEntity> list = this.schoolService.queryList(query);
        return R.ok().put("list", list);
    }
}
